package com.esiag.pdsteam.treatcomplexinfo;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.derby.tools.sysinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.esiag.pdsteam.dao.InformationDao;
import com.esiag.pdsteam.entities.Information;
import com.esiag.pdsteam.service.InformationService;


public class FindComplexInfo {
	 ApplicationContext context = 
	            new ClassPathXmlApplicationContext("classpath:test-context/FindComplexInfo-context.xml");
	       
	@Autowired
	InformationDao informationDao=context.getBean(InformationDao.class);
	@Autowired
	InformationService informationService=context.getBean(InformationService.class);
	
	private Map<Integer, Map<Integer,Information>> _piles = Collections.synchronizedMap(new HashMap<Integer, Map<Integer,Information>>());
	private Map<Integer, Information> piles = Collections.synchronizedMap(new HashMap<Integer, Information>());

	
	public List<Information> ComplexInfoFound(Information  information)
	{
		if(informationService.getById(information.getId()).getTypeComlexInfo().getTyp().equals("Critique"))
		{
			return informationService.getById(information.getId()).getSourceInformation();
		}

		return informationService.getById(information.getId()).getSourceInformation();
	}

	public void manageComplexInfo(Information information)
	{

		List<Information> l = ComplexInfoFound(information);

		if(l.size()==0)
		{
			Map<Integer,Information> li = new HashMap<Integer, Information>();
			li.put(information.getId(), information);
			_piles.put(0, li);
		}
		for(int i=0;i<l.size();i++)
		{
			synchronized (_piles) {
				Map<Integer,Information> list = _piles.get(l.get(i).getId());
				//System.out.println(list);
				if(list == null){
					list = new HashMap<Integer, Information>();
					_piles.put(l.get(i).getId(), list);
				}
				list.put(information.getId(),information);	
				System.out.println("add : " + list);
			}
		}
		System.out.println("information 1 : "+informationService.getById(1));
		System.out.println("information 5 : "+informationService.getById(5));
		
	}


	public Map<Integer, Map<Integer, Information>> get_piles() {
		return _piles;
	}

}





